var $ = exports

var _ = require("rosa/html4")
var stdTemplate = require("./template").standard

$.setup = function(sandro, route) {
	
	var handler = function(request) {
		var code = request.getParameter("code") || "return 'ENTER CODE TO EVALUATE'"
		var result = org.mozilla.javascript.Context.getCurrentContext().evaluateString(
			{request: request, _:_}, 
			"(function() {"+code+"})()",
			"EVAL",
			1,
			null
		)
		return stdTemplate(
			_.div({id: "evalResult"},result),
			_.form({action:route, method:"POST"},
				_.textarea({name:"code", style:"width:98%; height:30em"}, code),
				_.input({type:"submit", value:"Run code"})
			)
		)
	}

	sandro.htmlRoute("GET", route, handler)
	sandro.htmlRoute("POST", route, handler)
}